package net
{

	import common.HTTPService;
	import common.loader.BaseLoader;

	import flash.events.Event;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import flash.utils.IDataInput;
	import flash.utils.IDataOutput;

	import globals.Setting;

	import net.message.BaseMessage;
	import net.message.GCUserMessage;
	import net.message.GC_Equip_ShowAvailableMessage;
	import net.message.GC_Hero_TraitUpMessage;
	import net.message.GC_Loading_HeroMessage;
	import net.message.GC_Loading_RoleMessage;

	public class ServiceManager
	{
		private var _messageMap:Dictionary;
		private var _server:HTTPService;

		public function ServiceManager()
		{
			_messageMap = new Dictionary();
		}

		public function init():void
		{
			_server = new HTTPService(Setting.SERVER_HOST);
			_server.handler = serviceHandler;

			/////////////////---注册 GC的消息-----///////////////
			//_messageMap[MessageType.GC_USER_INFO] = new GCUserMessage();
			_messageMap[MessageType.GC_Loading_Role] = new GC_Loading_RoleMessage();
			_messageMap[MessageType.GC_Loading_Hero] = new GC_Loading_HeroMessage();
			_messageMap[MessageType.GC_Equip_ShowAvailable] = new GC_Equip_ShowAvailableMessage();
			_messageMap[MessageType.GC_Hero_TraitUp] = new GC_Hero_TraitUpMessage();
		}

		public function send(message:BaseMessage):void
		{
			_server.send(message);
		}

		private function serviceHandler(data:IDataInput):void
		{
			var length:uint = data.readInt();
			var cmd:uint = data.readShort();
			trace("server response complete,the cmd value is: " + cmd);
			var message:BaseMessage = _messageMap[cmd];

			if(message != null)
			{
				var body:ByteArray = new ByteArray();
				data.readBytes(body);
				body.position = 0;
				message.readMessage(body);
				message.handlerMessage();
			}
		}
	}
}